package cn.edu.zjicm.wordsnet_d.fragment;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.data.RelationShip;
import cn.edu.zjicm.wordsnet_d.datapersistent.FileManager;
import cn.edu.zjicm.wordsnet_d.media.VoicePlayer;
import cn.edu.zjicm.wordsnet_d.util.TypefaceUtil;

public class WordDetailForLockFragment extends Fragment implements OnClickListener {

	public WordDetailForLockFragment(OneWord word) {
		this.mOneWord = word;
	}

	public WordDetailForLockFragment() {
	}

	private OneWord mOneWord;
	private List<RelationShip> mRelationShipList;
	public View wordView;
	private TextView mWordText, mWordPhonetic, mWordCn /* , mWordAff */;
	private TextView relationEn1, relationEn2, relationCn1, relationCn2;
	private ImageView mReadButton;
	private RelativeLayout playLemma;
	private LinearLayout wordDisplayView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		wordView = inflater.inflate(R.layout.fragment_word_detail_for_lock, null);
		mWordText = (TextView) wordView.findViewById(R.id.word_detail_word);
		mWordPhonetic = (TextView) wordView.findViewById(R.id.word_detail_phonetic);
		mWordCn = (TextView) wordView.findViewById(R.id.word_detail_cn);
		// mWordAff = (TextView) wordView.findViewById(R.id.word_aff);
		mReadButton = (ImageView) wordView.findViewById(R.id.read_button);
		playLemma = (RelativeLayout) wordView.findViewById(R.id.play_sound);
		relationEn1 = (TextView) wordView.findViewById(R.id.relation_en_string1);
		relationEn2 = (TextView) wordView.findViewById(R.id.relation_en_string2);
		relationCn1 = (TextView) wordView.findViewById(R.id.relation_cn_string1);
		relationCn2 = (TextView) wordView.findViewById(R.id.relation_cn_string2);
		wordDisplayView = (LinearLayout) wordView.findViewById(R.id.word_detail_view);

		if (mOneWord != null) {
			initView();
		}
		// wordView.setOnTouchListener(a);
		return wordView;
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		wordDisplayView.setVisibility(View.INVISIBLE);
	}

	public void initViewForNewWord(OneWord word) {
		this.mOneWord = word;
		initView();
	}

	private void initView() {
		mWordText.setText(mOneWord.getLemma());
		TypefaceUtil.getInstance(getActivity()).setTypeface(mWordPhonetic);
		mWordPhonetic.setText(mOneWord.getPhonetic());
		mWordCn.setText(mOneWord.getSensesSenior());
		// if(mOneWord.getAff_content() != null &&
		// mOneWord.getAff_content().length() > 0){
		// mWordAff.setText("[记]" + mOneWord.getAff_content());
		// mWordAff.setVisibility(View.VISIBLE);
		// } else
		// mWordAff.setVisibility(View.GONE);

		mRelationShipList = DataManager.getInstance().getRelationShipList(getActivity(), mOneWord.getIndex_word_id());
		reLoadRelationShip();

		// removeReadIcon();
		if (FileManager.isVoiceFileExist(getActivity(), mOneWord)) {
			mReadButton.setVisibility(View.VISIBLE);
			mReadButton.setOnClickListener(this);
			playLemma.setOnClickListener(this);
		} else {
			// removeReadIcon();
			mReadButton.setVisibility(View.GONE);
		}
		// wordView.setOnClickListener(this);
		wordView.setOnTouchListener(touchLinstener);
	}

	// // 隐藏发音图标
	// private void removeReadIcon() {
	// mReadButton.setBackgroundDrawable(null);
	// mReadButton.setPadding(0, 6, 0, 6);
	// LinearLayout.LayoutParams lp = new
	// LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
	// LinearLayout.LayoutParams.WRAP_CONTENT);
	// lp.setMargins(0, 0, 0, 0);
	// mWordPhonetic.setLayoutParams(lp);
	// }

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if ((view == mReadButton) || (view == playLemma)) {
			VoicePlayer.getInstance(getActivity()).playWithNoCheckVoiceFile(mOneWord);
		}
	}

	public void displayWordView() {
		// VoicePlayer.getInstance().play(mOneWord.getLemma());
		wordDisplayView.setVisibility(View.VISIBLE);
	}

	OnTouchListener touchLinstener = new OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				wordDisplayView.setVisibility(View.VISIBLE);
				break;
			/*
			 * case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL:
			 * wordDisplayView.setVisibility(View.INVISIBLE); break;
			 */
			}
			return true;
		}
	};

	public void reLoadRelationShip() {
		RelationShip rs1, rs2;
		try {
			if (mRelationShipList.size() >= 1) {
				rs1 = mRelationShipList.get(0);
				relationEn1.setText(rs1.getColloc_eng());
				relationCn1.setText(rs1.getColloc_cn());
			}
			if (mRelationShipList.size() >= 2) {
				rs2 = mRelationShipList.get(1);
				relationEn2.setText(rs2.getColloc_eng());
				relationCn2.setText(rs2.getColloc_cn());
			}
		} catch (Exception e) {
			Log.e("reLoadRelationShip抛出异常", "异常异常");
		}
	}

	// void createRelationEnContainerWithLink(LinearLayout relationEnContainer,
	// RelationShip rs) {
	// String[] rsEng = rs.getColloc_eng().split(" ");
	// String[] relation = rs.getWord_str().split(";");
	// final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
	// for (int i = 0; i < relation.length; i += 1) {
	// String[] s = relation[i].split(",");
	// if (s.length == 2)
	// map.put(Integer.valueOf(s[1]), Integer.valueOf(s[0]));
	// }
	// if (rsEng.length > 0) {
	// for (int i = 0; i < rsEng.length; i++) {
	// TextView tv = new TextView(getActivity());
	// tv.setGravity(Gravity.CENTER);
	// tv.setTextSize(16);
	// tv.setTextColor(getResources().getColor(R.color.word_text_color));
	// tv.setText(rsEng[i]);
	//
	// // int padding = (int) (5 *
	// // getActivity().getResources().getDisplayMetrics().density);
	// // tv.setPadding(padding, 0, padding, 0);
	// LinearLayout.LayoutParams para = new
	// LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
	// LayoutParams.WRAP_CONTENT);
	// para.rightMargin = (int) (10 *
	// getResources().getDisplayMetrics().density);
	// tv.setLayoutParams(para);
	// relationEnContainer.addView(tv);
	// }
	// }
	// }

}